<?php

/**
 * The Buildpress Admin Page
 *
 * @package Buildpress
 * @subpackage Administration
 */

if ( !current_user_can('edit_themes') ) {
	die('-1');
}

class buildpress_admin {
	
	 function init() {
		add_action( 'admin_menu', array('buildpress_admin', 'bp_add_admin_page' ) );		
	}	
	
	 function bp_add_admin_page() {
		 
		$admin_page = add_theme_page(__('Buildpress', 'bp'), __('Buildpress', 'bp'), 'edit_themes', basename(__FILE__), array('buildpress_admin','bp_show_admin_page'));
		
		//for demo purposes only !
		//add_submenu_page('profile.php', __('Buildpress', 'bp'), __('Buildpress', 'bp'), 'read',array('buildpress_admin','bp_show_admin_page'), array('buildpress_admin','bp_show_admin_page'));
		
		add_action( 'admin_head-' . $admin_page,  array('buildpress_admin', 'bp_admin_js') );	
		//add_action( 'admin_head',  array('buildpress_admin', 'bp_admin_js' ) );	
	}
	
	//include all js
	function bp_admin_js() {
	//include ajax.admin.js
	echo "\n" . '<script src="' . BP_URL . '/engine/admin/js/ajax.admin.js" type="text/javascript"></script>' . "\n";
	echo '<style> .active { background:#eee } </style>';
	?>
    <script type="text/javascript"> 
	var nonce = "<?php echo js_escape( wp_create_nonce( 'ajaxnonce' ) ); ?>";
	</script>
    <?php
	}
	
	function bp_show_admin_page() {
		global $b;
		
	?>    
        <div class="wrap">
        <div id="icon-themes" class="icon32"><br /></div>
        <h2>Buildpress Admin Page</h2>
        </div>
            <?php            
			$currentThemeData = $b->getTheme( $b->getActivatedThemeID() );
            $themeData = $b->getTheme();
            ?>
            <h2>Create a new Theme</h2>
            <div>
                <form action="<?php echo BP_URL . '/engine/admin/buildpress.admin.builder.php'; ?>" method="post">
                <label for="themename">Theme Name : </label>
                <input type="text" id="theme_name" name="theme_name" />
                <input type="hidden" id="theme_buildpress" name="theme_buildpress" value="1" />
                <input type="submit" value="Create New Theme" />
                </form>
            </div>
            <!--
            <h3>Current Theme</h3>
            <div id="current-theme" style="overflow:hidden">
            <img alt="Current theme preview" src="http://localhost/wp28/wp-content/themes/buildpress/screenshot.png"/>
            <h4><?php echo $currentThemeData['themename']; ?></h4>
            <p class="theme-description"> <?php echo $currentThemeData['themedesc']; ?></p>
            </div>
            -->
            <h2>Available Themes</h2>
            <div class="clear"/></div>
            <table id="all-plugins-table" class="widefat" cellspacing="1">
            <thead>
            <tr>
            <th class="manage-column" width="30%">Theme Name</th><th class="manage-column">Theme Description</th>
            </tr>
            </thead>
            <tbody>
            <?php if($themeData) : foreach($themeData as $key=>$t) :
            
             if($key == $b->getActivatedThemeID()) { 
                 $activatedClass = 'class="active"';
                 $activatedText = '<a class="activate_theme" href="javascript:void(0);">Activated</a>'; 
             }	 else { 
                 $activatedClass = "";  
                 $activatedText = '<a class="activate_theme" href="javascript:void(0);">Activate</a>';  
             }//don`t show activated theme
            ?>
            <tr id="theme-<?php echo $key; ?>" <?php echo $activatedClass; ?>>
            <td><strong><?php echo $t['themename']; ?></strong></td>
            <td>

             <div class="row-actions-visible">
             <!--
             <span class="0">
             <a href="#">Preview</a> | 
             </span>
             -->
             <span class="1"><?php echo $activatedText; ?> | </span>
             <!--
             <span class="2"> <form action="<?php echo BP_URL . '/engine/admin/buildpress.admin.builder.php&edit=1'; ?>" method="post">
             <input type="hidden" id="theme_name" name="theme_name" value="<?php echo $t['themename']; ?>" />
                <input type="hidden" id="theme_edit" name="theme_edit" value="<?php echo $key; ?>" />
                <input type="hidden" id="theme_buildpress" name="theme_buildpress" value="1" />
                <input type="submit" value="Edit" />
                </form> | </span>
                -->
              <span class="2"><a href="<?php echo BP_URL . '/engine/admin/buildpress.admin.builder.php?edit='.$key.''; ?>" class="edit_theme">Edit</a> | </span>
                
             <span class="3"><a href="javascript:void(0);" class="delete_theme">Delete</a></span>
             
             <span class="progress" style="display:none;"><img src="<?php bloginfo('template_directory'); ?>/engine/admin/images/loader.gif" alt="Loading" title='Loading' /></span>
        </div>
            </td>
            </tr>
            <?php endforeach; else: ?>
            <tr>
            <td colspan="2" align="center">No themes are created yet</td>
            </tr>
            <?php endif; ?>
            </tbody>
            </table>
            <pre>
            <?php // print_r($themeData); ?>
            </pre>
            <?php
	}
}

buildpress_admin::init();

